/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.wicket.pages;

import java.io.IOException;

import com.googlecode.jsimplemarket.model.SpatiuComercial;
import com.googlecode.jsimplemarket.model.Utilizator;
import com.googlecode.jsimplemarket.service.GenericService;
import com.googlecode.jsimplemarket.wicket.SecuredWebSession;
import com.googlecode.jsimplemarket.wicket.zzz.ZZZListPage;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.security.core.GrantedAuthority;
import org.wicketstuff.security.components.SecureWebPage;

/**
 *
 * @author Alexandru.Codescu
 */
public class DefaultPageTemplate extends SecureWebPage {
	
	@SpringBean(name = "genericService")
    private GenericService<SpatiuComercial> spatiuComercialService;
	
    private Link createNavigationLink(String id, final Class<? extends WebPage> pageClass, String... roles) {
        Link link = new Link(id) {
            @Override
            public void onClick() {
                setResponsePage(pageClass);
            }
        };
        boolean visible = false;
        for (GrantedAuthority authority: getUtilizator().getAuthorities()) {
            for (String role : roles) {
                if (authority.getAuthority().equalsIgnoreCase(role)) {
                    visible = true;
                    break;
                }
            }
            if (visible) {
                break;
            }
        }
        
        link.setVisible(visible);
        return link;
    }
    
    

    protected Utilizator getUtilizator() {
        return ((SecuredWebSession) getSession()).getUserDetail().getUtilizator();
    }
    
    protected String getUsername() {
        return ((SecuredWebSession) getSession()).getUserDetail().getUsername();
    }
    
    public DefaultPageTemplate() {
        Link lkLogout = new Link("logoutLink") {
            @Override
            public void onClick() {
                getSession().invalidateNow();
                setResponsePage(LoginPage.class);
            }
        };
        final TextField<String> txtSearch = new TextField<String>("txtSearch", Model.of(""));
//        Link btnSearch = new Link("btnSearch") {
//            @Override
//            public void onClick() {
//                //getSession().invalidateNow();
//            	txtSearch.processInput();
//            	final String searchValue = txtSearch.getModelObject();
//            	final SpatiuComercial scid = spatiuComercialService.getById(SpatiuComercial.class, Long.valueOf(searchValue));
//                setResponsePage(new TarabaViewPage(scid.getId()));
//            }
//        };
        Form<?> formSearch = new Form<Void>("formSearch") {
 
			@Override
			protected void onSubmit() {
 
				//txtSearch.processInput();
            	final String searchValue = txtSearch.getModelObject();
            	
            	try {
            		final Long intValue = Long.valueOf(searchValue);
            		if (intValue >=1 && intValue <=140) {
            			final SpatiuComercial scid = spatiuComercialService.getById(SpatiuComercial.class, Long.valueOf(searchValue));
                        setResponsePage(new TarabaViewPage(scid.getId()));
            		}
            	            	
            	} catch (NumberFormatException e) {
            	    //System.err.println("Caught IOException: " + e.getMessage());
            		setResponsePage(new AdminPage());
            	}
            	
//            	final SpatiuComercial scid = spatiuComercialService.getById(SpatiuComercial.class, Long.valueOf(searchValue));
//                setResponsePage(new TarabaViewPage(scid.getId()));
 
			}
 
		};
        formSearch.add(txtSearch);
        
        Label lblNumeUtilizator = new Label("username", getUsername());
        Link linkLogo = createNavigationLink("logoLink", PiataViewPage.class, "ROLE_ADMIN");
        Link linkDashboard = createNavigationLink("dashboard", AdminPage.class, "ROLE_ADMIN");
        Link linkDash = createNavigationLink("dashboardLink", AdminPage.class, "ROLE_ADMIN");
        Link linkUtilizatori = createNavigationLink("utilizatori", UtilizatoriViewPage.class, "ROLE_ADMIN");
        Link linkNomenclatoare = createNavigationLink("nomenclatoare", NomenclatoarePage.class, "ROLE_ADMIN");
        Link linkSpatii = createNavigationLink("spatii", SpatiuViewPage.class, "ROLE_ADMIN");
        Link linkPiata = createNavigationLink("piata", PiataViewPage.class, "ROLE_ADMIN");
        Link linkFirme = createNavigationLink("firme", FirmaViewPage.class, "ROLE_ADMIN");
        Link raportIncasari = createNavigationLink("raportIncasari", RaportIncasariZilnicePage.class, "ROLE_ADMIN");
        Link raportTarabe = createNavigationLink("raportTarabe", RaportTarabePage.class, "ROLE_ADMIN");
        Link raportAbsente = createNavigationLink("raportAbsente", RaportAbsentePage.class, "ROLE_ADMIN");
        Link raportZzz = createNavigationLink("raportzzz", ZZZListPage.class, "ROLE_ADMIN");
        
        add(linkLogo, lkLogout, lblNumeUtilizator,formSearch);
        add(linkDashboard, linkDash, linkUtilizatori, linkNomenclatoare, linkSpatii, linkPiata, linkFirme);
        add(raportIncasari, raportTarabe, raportAbsente, raportZzz); 
}
}
